<?php
/**
 * 获取指定URL地址的文章内容
 * @param  string $request_url
 * @param  array $encode 默认编码
 * **/
function getUrlFile($request_url, $encode = "UTF-8") {
	require COM_PATH.'Library/Vendor/HtmlParser/config.php';
	require COM_PATH.'Library/Vendor/HtmlParser/Readability.php';
	$request_url_hash = md5($request_url);
	$request_url_cache_file = sprintf(DIR_CACHE."/%s.url", $request_url_hash);

	// 缓存请求数据，避免重复请求
	if (file_exists($request_url_cache_file) && (time() - filemtime($request_url_cache_file) < CACHE_TIME)) {
		
	    $source = file_get_contents($request_url_cache_file);
	    
	} else {
	
	    $handle = curl_init();
	    curl_setopt_array($handle, array(
		    CURLOPT_USERAGENT => USER_AGENT,
	        CURLOPT_FOLLOWLOCATION => true,
	        CURLOPT_HEADER  => false,
	        CURLOPT_HTTPGET => true,
	        CURLOPT_RETURNTRANSFER => true,
	        CURLOPT_TIMEOUT => 30,
	        CURLOPT_URL => $request_url
	    ));
	
	    $source = curl_exec($handle);
	    curl_close($handle);
	
	    // Write request data into cache file.
	    @file_put_contents($request_url_cache_file, $source);
	}

	// 判断编码
	//if (!$charset = mb_detect_encoding($source)) {
	//}
	
	preg_match("/charset=([\w|\-]+);?/", $source, $match);
	$charset = isset($match[1]) ? $match[1] : 'utf-8';

	/**
	 * 获取 HTML 内容后，解析主体内容
	 */
	$Readability = new Readability($source, $charset);
	$Data = $Readability->getContent();
	
	return $Data;
}